package com.example.mydemo.controller.hc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.entity.Menu;
import com.example.mydemo.entity.SysParam;
import com.example.mydemo.entity.domain.MenuDTO;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.entity.domain.dtree.CheckArr;
import com.example.mydemo.entity.domain.dtree.DTree;
import com.example.mydemo.entity.domain.dtree.DTreeResponse;
import com.example.mydemo.mapper.MenuMapper;
import com.example.mydemo.model.tableVO;
import com.example.mydemo.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author syf
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/menu")
public class MenuController {


    @Autowired
    private IMenuService iMenuService;
    @Autowired
    private MenuMapper menuMapper;
    @GetMapping("/getMenuById")
    @ResponseBody
    public ResponseDTO<Menu> getList(@RequestParam Long id) {
        Menu sysParam = menuMapper.selectOne(new QueryWrapper<Menu>().eq("menu_id", id).eq("is_delete", 0));
        if (sysParam.getIsDelete()==0L){
            return new ResponseDTO<>(200, "获取信息成功！", sysParam);
        }else {
            return new ResponseDTO<>(200, "获取信息失败！", sysParam);
        }
    }
    @GetMapping("/getmenupage")
    public tableVO getsysparmpage(@RequestParam Long page, @RequestParam Long limit, @RequestParam(defaultValue = "") String searchParams,@RequestParam(defaultValue = "") String searchParams2) {
        Page<Menu> codepage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        tableVO a = new tableVO();
        if (!Objects.equals(searchParams2, "") && !Objects.equals(searchParams, "")){
            IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0).eq("parent_id", searchParams2).like("title", searchParams));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }else if(!Objects.equals(searchParams, "")) {
            IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0).like("title", searchParams));
            a.setCode(0L);
        a.setMsg("");
        a.setData(sysParamPagePage.getRecords());
        a.setCount(sysParamPagePage.getTotal());
        return a;
        }else if(!Objects.equals(searchParams2, "")) {
            IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0).eq("parent_id", searchParams2));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }else {
            IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0));
            a.setCode(0L);
            a.setMsg("");
            a.setData(sysParamPagePage.getRecords());
            a.setCount(sysParamPagePage.getTotal());
            return a;
        }
    }
    @GetMapping("/getmenupage2")
    public DTreeResponse getMenuTree() {
        Page<Menu> codepage = new Page<>(1, 1000); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0));
        DTreeResponse a = new DTreeResponse();
        List<Menu> b =sysParamPagePage.getRecords();
        List<DTree> c = new ArrayList<>();
        List<CheckArr> checkArrs =new ArrayList<>();
        CheckArr x = new CheckArr("0","0");
        checkArrs.add(x);
        for (Menu n : b){
            String id,parentId;
            id = String.valueOf(n.getMenuId());
            parentId= String.valueOf(n.getParentId());
            DTree tree = new DTree(id,parentId,n.getTitle(),"-1",null,checkArrs);
            c.add(tree);
        }
        a.setData(c);
        return a;
    }
    @GetMapping("/getmenupage3")
    public tableVO getMenupage() {
        Page<Menu> codepage = new Page<>(1, 1000); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0));
        List<MenuDTO> c = new ArrayList<>();
        tableVO a = new tableVO();
        a.setCode(0L);
        for(Menu n :sysParamPagePage.getRecords()){
            MenuDTO x = MenuDTO.from(n);
            c.add(x);
        }
        a.setMsg("操作成功");
        a.setData(c);
        a.setCount(sysParamPagePage.getTotal());
//        ResponseEntity<List<SysParm>> a =ResponseEntity.ok(b);
        return a;
    }
    @GetMapping("/getmenupage1")
    public tableVO getsysparmpage() {
        Page<Menu> codepage = new Page<>(1, 1000); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<Menu> sysParamPagePage = menuMapper.selectPage(codepage, new QueryWrapper<Menu>().eq("is_delete", 0));
        List<Menu> b = menuMapper.selectList(null);
//        redisUtil.setObjectWithExpire("LIST",b,30);
        tableVO a = new tableVO();
        a.setCode(0L);
        a.setMsg("");
//        if (searchParams ==""){
        a.setData(sysParamPagePage.getRecords());
//        }else {
//            a.setData(iMenuService.selectByName(searchParams));
//        }
        a.setMsg("操作成功");
        a.setCount((long) b.size());
//        ResponseEntity<List<SysParm>> a =ResponseEntity.ok(b);
        return a;
    }
    @PostMapping("/update-db")
    public ResponseEntity<Boolean> updateDbParam(
            @RequestParam String title,
            @RequestParam String href,
            @RequestParam String icon,
            @RequestParam Long menuId,
            @RequestParam Long parentId,
            @RequestParam Long sort
    ) {
        Menu sysParam = new Menu();
        sysParam.setHref(href);
        sysParam.setIcon(icon);
        sysParam.setTitle(title);
        sysParam.setParentId(parentId);
        sysParam.setSort(Math.toIntExact(sort));
        int updated = menuMapper.update(
                sysParam, // 显式传递实体对象
                new UpdateWrapper<Menu>().eq("menu_id", menuId)
        );
        return ResponseEntity.ok(updated > 0);
    }
    @PostMapping("/menuAdd")
    @ResponseBody
    public ResponseDTO<Menu> paramAdd(@RequestParam String title, @RequestParam String href, @RequestParam String icon, @RequestParam Long parentId, @RequestParam Long sort) {
        Menu sysParam = new Menu();
        sysParam.setTitle(title);
        sysParam.setHref(href);
        sysParam.setIcon(icon);
        sysParam.setParentId(parentId);
        sysParam.setSort(Math.toIntExact(sort));
        if (iMenuService.save(sysParam)){
            return new ResponseDTO<>(200, "添加信息成功！", sysParam);
        }
        return new ResponseDTO<>(404, "添加信息失败！", sysParam);
    }
    @PostMapping("/menuDelete")
    @ResponseBody
    public ResponseDTO<Menu> paramDelete(@RequestParam Long id) {
        Menu sysParam = new Menu();
        sysParam.setIsDelete(1L);
        int updated = menuMapper.update(
                sysParam, // 显式传递实体对象
                new UpdateWrapper<Menu>().eq("menu_id", id)
        );
        return new ResponseDTO<Menu>(200, "添加信息成功！", sysParam);
    }
}
